﻿using System.Threading;
using NUnit.Framework;
using Present.Commands.Fluent;

namespace Present.Commands.Tests.Decorators
{
    [TestFixture]
    public class CancelOnNewExecutionAsyncActionDecoratorTestCase : TestCase
    {
        [Test]
        public void ShouldCancelCurrentOperationWhenNewOperationExecuted()
        {
            EnableAsyncCalls();
            var cancelled = new AutoResetEvent(false);
            var action = AsyncAction.FromDelegates()
                .Do(e =>
                        {
                            while (!e.CancelRequested) { }
                            cancelled.Set();
                        })
                .CancelOnNewExecution()
                .ToAction();


            var command = Command.Async(action).ToCommand();
            command.Execute();
            command.Execute();
            Assert.IsTrue(cancelled.WaitOne(1000));
        }
    }
}